//
//  ZWInteractionAController.m
//  iOSH5Interaction
//
//  Created by zaiwei on 2017/11/5.
//  Copyright © 2017年 home. All rights reserved.
//

#import "ZWInteractionAController.h"

@interface ZWInteractionAController ()<UIWebViewDelegate>
@property (nonatomic, strong) UIWebView *webView;

@end

@implementation ZWInteractionAController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"交互方式A";
    self.view.backgroundColor = [UIColor lightGrayColor];
    
    
    _webView = [[UIWebView alloc] init];
    _webView.frame = CGRectMake(0, 0, 375, 375);
    _webView.delegate = self;
    [self.view addSubview:_webView];
    
    UIButton *button1 = [UIButton buttonWithType:UIButtonTypeSystem];
    [button1 setTitle:@"修改html的背景色" forState:UIControlStateNormal];
    button1.frame = CGRectMake(100, CGRectGetMaxY(_webView.frame)+5, 200, 50);
    button1.backgroundColor = [UIColor yellowColor];
    [button1 addTarget:self action:@selector(button1Click) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button1];
    
    UIButton *button2 = [UIButton buttonWithType:UIButtonTypeSystem];
    [button2 setTitle:@"html弹框" forState:UIControlStateNormal];
    button2.frame = CGRectMake(100, CGRectGetMaxY(button1.frame)+5, 200, 50);
    button2.backgroundColor = [UIColor yellowColor];
    [button2 addTarget:self action:@selector(button2Click) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button2];
    
    
    
    NSString* htmlPath = [[NSBundle mainBundle] pathForResource:@"webAppA" ofType:@"html"];
    NSString* appHtml = [NSString stringWithContentsOfFile:htmlPath encoding:NSUTF8StringEncoding error:nil];
    NSURL *baseURL = [NSURL fileURLWithPath:htmlPath];
    [_webView loadHTMLString:appHtml baseURL:baseURL];
    
    
}

#pragma mark - userAction
-(void)button1Click{

    [_webView stringByEvaluatingJavaScriptFromString:@"document.getElementsByTagName('body')[0].style.background='#ffe'"];//调用JS代码修改背景色
}
-(void)button2Click{
    //H5 要提前定义好方法 function alertTest(str)
     [_webView stringByEvaluatingJavaScriptFromString:@"alertTest('hello world!')"];
}


#pragma mark - for h5
-(void)handleFromH5A:(NSString *)parm{
    NSLog(@"参数为: %@",parm);
}
-(void)handleFromH5B{
    NSLog(@"无参数调用");
}
/**
 webView 的代理方法可以使用 NSURLProtocol 类来全局控制

 */
#pragma mark - <UIWebViewDelegate>
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType{
    //request.URL.absoluteString:  iostest://show?alert=showAndroidToast
    //request.URL.scheme:  iostest
    //request.URL.resourceSpecifier:  //show?alert=showAndroidToast
    //request.URL.host: show
    //request.URL.query: alert=showAndroidToast
//    NSLog(@"-- %@",request.URL.absoluteString);
//    NSLog(@"-- %@",request.URL.scheme);
//    NSLog(@"-- %@",request.URL.resourceSpecifier);
    
    
    NSLog(@"url: %@",request.URL);
    //方法一 拿到信息判断 然后iOS做处理
    if ([request.URL.scheme isEqualToString:@"iostest"]) {//如果是我们自定义的scheme协议
        if ([request.URL.host isEqualToString:@"test1"]) {
            NSLog(@"web上测试按钮1点击 并传来参数: %@",request.URL.query);
        }else if ([request.URL.host isEqualToString:@"test2"]){
            NSLog(@"web上测试按钮2点击 并传来参数: %@",request.URL.query);
        }else{
            NSLog(@"web上其他事件响应 并传来参数: %@",request.URL.query);
        }
    }
    
    //方法二 直接定义好方法给H5调用
    else if ([request.URL.scheme isEqualToString:@"activetest"]){
        if (request.URL.query) {
            //有参数
            SEL selectorB = NSSelectorFromString([request.URL.host stringByAppendingString:@":"]);
            if([self respondsToSelector:selectorB]){
                [self performSelector:selectorB withObject:request.URL.query];
            }else{
                NSLog(@"此方法 iOS没有定义: %@:",request.URL.host);
            }
        }else{
            SEL selector = NSSelectorFromString(request.URL.host);
            //无参数
            if ([self respondsToSelector:selector]) {
                [self performSelector:selector];
            }else{
                NSLog(@"此方法 iOS没有定义: %@",request.URL.host);
            }
        }
        
    }
    return YES;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
